/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui;

import java.awt.BorderLayout;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JToolBar;

import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventTopicSubscriber;
import org.jdesktop.swingx.JXPanel;
import org.shake.lastfm.Resources;
import org.shake.lastfm.event.EventTopic;
import org.shake.lastfm.event.SessionStartFailedEvent;
import org.shake.lastfm.event.SessionStartedEvent;
import org.shake.lastfm.ui.action.CaptureAction;
import org.shake.lastfm.ui.action.ConnectionAction;
import org.shake.lastfm.ui.action.PreferencesAction;

public class ToolsPane extends JXPanel
{
    private static final long serialVersionUID = 332457823629650710L;
    
    private JToolBar toolBar;
    private AbstractAction connectionAction;
    private AbstractAction captureAction;
    private AbstractAction preferencesAction;
    
    public ToolsPane()
    {
        super();
        buildUI();
        
        AnnotationProcessor.process(this);
    }

    private void buildUI()
    {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));

        this.toolBar = new JToolBar();
        this.toolBar.setRollover(true);
        this.toolBar.setFloatable(false);
        
        this.connectionAction = new ConnectionAction();
        this.toolBar.add(this.connectionAction);
        
        this.preferencesAction = new PreferencesAction();
        this.toolBar.add(this.preferencesAction);
        
        this.captureAction = new CaptureAction();
        this.toolBar.add(this.captureAction);
        
        add(this.toolBar, BorderLayout.CENTER);
    }
    
    @EventTopicSubscriber(topic=EventTopic.SESSION_STARTED_EVENT)
    public void onSessionStarted(String topic, SessionStartedEvent event)
    {
        this.connectionAction.putValue(Action.SMALL_ICON, Resources.getIcon("icon.disconnected.22x22"));
        this.captureAction.setEnabled(true);
    }
    
    @EventTopicSubscriber(topic=EventTopic.SESSION_START_FAILED_EVENT)
    public void onSessionStartFailed(String topic, SessionStartFailedEvent event)
    {
        this.connectionAction.putValue(Action.SMALL_ICON, Resources.getIcon("icon.connected.22x22"));
        this.captureAction.setEnabled(false);
        this.connectionAction.setEnabled(true);
    }
}